<?php

/**
 * tag actions.
 *
 * @package    blogallery
 * @subpackage tag
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 2288 2006-10-02 15:22:13Z fabien $
 */
class TagActions extends sfActions
{
  public function executeIndex()
  {
    return $this->forward('tag', 'list');
  }

  public function executeList()
  {
    $this->processSort();

    $this->processFilters();

    $this->filters = $this->getUser()->getAttributeHolder()->getAll('sf_admin/tag/filters');

    // pager
    $this->pager = new sfPropelPager('Tag', 20);
    $c = new Criteria();
    $this->addSortCriteria($c);
    $this->addFiltersCriteria($c);
    $this->pager->setCriteria($c);
    $this->pager->setPage($this->getRequestParameter('page', 1));
    $this->pager->init();
  }

  public function executeCreate()
  {
    return $this->forward('tag', 'edit');
  }

  public function executeSave()
  {
    return $this->forward('tag', 'edit');
  }

  public function executeEdit()
  {
    $this->languages = LanguagePeer::doSelect(new Criteria());
    
    $this->tag = $this->getTagOrCreate();

    if ($this->getRequest()->getMethod() == sfRequest::POST)
    {
      $this->updateTagFromRequest();

      $this->saveTag($this->tag);

      $this->setFlash('notice', 'Your modifications have been saved');

      if ($this->getRequestParameter('save_and_add'))
      {
        return $this->redirect('tag/create');
      }
      else if ($this->getRequestParameter('save_and_list'))
      {
        return $this->redirect('tag/list');
      }
      else
      {
        return $this->redirect('tag/edit?id='.$this->tag->getId());
      }
    }
    else
    {
      $this->labels = $this->getLabels();
    }
  }

  public function executeDelete()
  {
    $this->tag = TagPeer::retrieveByPk($this->getRequestParameter('id'));
    $this->forward404Unless($this->tag);

    try
    {
      $this->deleteTag($this->tag);
    }
    catch (PropelException $e)
    {
      $this->getRequest()->setError('delete', 'Could not delete the selected Tag. Make sure it does not have any associated items.');
      return $this->forward('tag', 'list');
    }

    return $this->redirect('tag/list');
  }

  public function handleErrorEdit()
  {
    $this->preExecute();
    $this->tag = $this->getTagOrCreate();
    $this->updateTagFromRequest();

    $this->labels = $this->getLabels();

    return sfView::SUCCESS;
  }

  protected function saveTag($tag)
  {
    $tag->save();

  }

  protected function deleteTag($tag)
  {
    $tag->delete();
  }

  protected function updateTagFromRequest()
  {
    $tag = $this->getRequestParameter('tag');

    if (isset($tag['handle']))
    {
      $this->tag->setHandle($tag['handle']);
    }
    if (isset($tag['i18n']))
    {   
      $this->setI18n($tag['i18n']);
    }
  }
  
  protected function setI18n($i18ns)
  {
    //set i18n tag fields
    foreach($i18ns as $culture => $i18n)
    {
      $tag_i18n = TagI18nPeer::retrieveByPk($this->tag->getId(), $culture);
      
      if(!$tag_i18n instanceof TagI18n)
      {
        $tag_i18n = new TagI18n();
      }
      
      $tag_i18n->setId($this->tag->getId());
      $tag_i18n->setCulture($culture);
      $tag_i18n->setName($i18n['name']); 
      $tag_i18n->save();
    }       
  }

  protected function getTagOrCreate($id = 'id')
  {
    if (!$this->getRequestParameter($id))
    {
      $tag = new Tag();
    }
    else
    {
      $tag = TagPeer::retrieveByPk($this->getRequestParameter($id));

      $this->forward404Unless($tag);
    }

    return $tag;
  }

  protected function processFilters()
  {
    if ($this->getRequest()->hasParameter('filter'))
    {
      $filters = $this->getRequestParameter('filters');

      $this->getUser()->getAttributeHolder()->removeNamespace('sf_admin/tag/filters');
      $this->getUser()->getAttributeHolder()->add($filters, 'sf_admin/tag/filters');
    }
  }

  protected function processSort()
  {
    if ($this->getRequestParameter('sort'))
    {
      $this->getUser()->setAttribute('sort', $this->getRequestParameter('sort'), 'sf_admin/tag/sort');
      $this->getUser()->setAttribute('type', $this->getRequestParameter('type', 'asc'), 'sf_admin/tag/sort');
    }

    if (!$this->getUser()->getAttribute('sort', null, 'sf_admin/tag/sort'))
    {
    }
  }

  protected function addFiltersCriteria($c)
  {
    if (isset($this->filters['handle_is_empty']))
    {
      $criterion = $c->getNewCriterion(TagPeer::HANDLE, '');
      $criterion->addOr($c->getNewCriterion(TagPeer::HANDLE, null, Criteria::ISNULL));
      $c->add($criterion);
    }
    else if (isset($this->filters['handle']) && $this->filters['handle'] !== '')
    {
      $c->add(TagPeer::HANDLE, strtr($this->filters['handle'], '*', '%'), Criteria::LIKE);
    }
  }

  protected function addSortCriteria($c)
  {
    if ($sort_column = $this->getUser()->getAttribute('sort', null, 'sf_admin/tag/sort'))
    {
      $sort_column = TagPeer::translateFieldName($sort_column, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_COLNAME);
      if ($this->getUser()->getAttribute('type', null, 'sf_admin/tag/sort') == 'asc')
      {
        $c->addAscendingOrderByColumn($sort_column);
      }
      else
      {
        $c->addDescendingOrderByColumn($sort_column);
      }
    }
  }

  protected function getLabels()
  {
    return array(
      'tag{handle}' => 'Handle:',
      'tag{i18n}' => 'Name:',
    );
  }
}
